#include "mainview.h"
#include "ui_mainview.h"

#include <QPainter>
#include <QPen>
#include <QImage>
#include <QDebug>

MainView::MainView(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MainView)
{
    ui->setupUi(this);

    ui->photoBtn->setStyleSheet("background-color:transparent;border: 0px solid;border-image:url(:/res/photo.png);");
    ui->cameraBtn->setStyleSheet("background-color:transparent;border: 0px solid;border-image:url(:/res/camera.png);");
    ui->videoBtn->setStyleSheet("background-color:transparent;border: 0px solid;border-image:url(:/res/video.png);");

    keyActive();
    updateIcon();

    if(space::checkCamera())
    {
        this->show();
        keyDisative();
        MainWindow *m = new MainWindow(this);
        m->show();
        connect(m, &MainWindow::sigClose, this, [&]{keyActive();this->show();});
    }
    else
        this->show();
}

MainView::~MainView()
{
    delete ui;
}

void MainView::KeyEvent(Key_t type, int key)
{
    //qDebug() << "MainView:key=" << key << " type=" << type;
    if(key == 0)
    {
        if(type == KEY_PRESS)
        {
            if(index == 0)
                index = 2;
            else index--;
            updateIcon();
        }
    }
    else if(key == 1)
    {
        if(type == KEY_PRESS)
        {
            if(index == 0)
            {
                keyDisative();
                Photo *p = new Photo(this);
                p->show();
                connect(p, &Photo::sigClose, this, [&]{keyActive();});
            }
            else if(index == 1)
            {
                keyDisative();
                if(space::checkCamera())
                {
                    MainWindow *m = new MainWindow(this);
                    m->show();
                    connect(m, &MainWindow::sigClose, this, [&]{keyActive();});
                }
                else
                {
                    QString data = "请插入摄像头";
                    TipWidget *t = new TipWidget(data);
                    t->show();
                    connect(t, &TipWidget::sigClose, this, [&]{keyActive();});
                }
            }
            else if(index == 2)
            {
                keyDisative();
                VideoList *v= new VideoList(this);
                v->show();
                connect(v, &VideoList::sigClose, this, [&]{keyActive();});
            }
        }
    }
    else if(key == 2)
    {
        if(type == KEY_PRESS)
        {
            index++;
            index %= 3;
            updateIcon();
        }
    }
}


void MainView::updateIcon()
{
    if(index == 0)
    {
        ui->photo_label->setStyleSheet("color:#2094fa;");
        ui->camera_label->setStyleSheet("color:#fff;");
        ui->video_label->setStyleSheet("color:#fff;");
    }
    else if(index == 1)
    {
        ui->camera_label->setStyleSheet("color:#2094fa;");
        ui->photo_label->setStyleSheet("color:#fff;");
        ui->video_label->setStyleSheet("color:#fff;");
    }
    else if(index == 2)
    {
        ui->video_label->setStyleSheet("color:#2094fa;");
        ui->photo_label->setStyleSheet("color:#fff;");
        ui->camera_label->setStyleSheet("color:#fff;");
    }
}


void MainView::on_photoBtn_clicked()
{
    index = 0;
    updateIcon();
    KeyEvent(KEY_PRESS, 1);
}

void MainView::on_cameraBtn_clicked()
{
    index = 1;
    updateIcon();
    KeyEvent(KEY_PRESS, 1);
}

void MainView::on_videoBtn_clicked()
{
    index = 2;
    updateIcon();
    KeyEvent(KEY_PRESS, 1);
}

void MainView::keyActive()
{
    connect(KeyThread::Get(), &KeyThread::keyEvent, this, &MainView::KeyEvent);
}

void MainView::keyDisative()
{
    disconnect(KeyThread::Get(), &KeyThread::keyEvent, this, &MainView::KeyEvent);
}

